package com.bidv.wcm;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bidv.util.DateUtil;
import com.bidv.util.StringUtil;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.ImageComponent;
import com.ibm.workplace.wcm.api.TextComponent;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;

@WebServlet("/Detail")
public class Detail extends HttpServlet {
	private static final String HREF = "href";
	private static final String MODIFIED_DATE = "modifiedDate";
	private static final String LABEL_DOC = "label";
	private static final String DETAIL_JSP_WCM_WPS_WCM_CONNECT = "detail.jsp?wcm=/wps/wcm/connect/";
	private static final String TEMPLATE = "TempNews";

	public Detail() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			response.setContentType("application/json");
			// bien json
			PrintWriter out = response.getWriter();
			JSONArray jsonArray = new JSONArray();
			JSONObject json = null;

			String fromPage =request.getParameter("FromPage");
			int pageSize = Integer.parseInt(request.getParameter("PageSize"));
			String site = request.getParameter("Site");
			String library = request.getParameter("Library");
			int k = 1;

			DocumentId siblingId = null;
			DocumentId authoringTmpl = null;
			DocumentId siteArea = null;
			DocumentIdIterator authoringIterator = null;
			DocumentIdIterator siteAreaIterator = null;
			DocumentId<Content> contentid = null;
			Content content = null;

			Workspace myworkspace = WCM_API.getRepository()
					.getSystemWorkspace();
			myworkspace.login();

			DocumentLibrary MyLibrary = myworkspace.getDocumentLibrary(library);
			myworkspace.setCurrentDocumentLibrary(MyLibrary);

			// find Authoring template
			authoringIterator = myworkspace.findByName(
					DocumentTypes.AuthoringTemplate, TEMPLATE);
			// get authoring template of lib
			authoringTmpl = authoringIterator.next();
			// find Sitearea
			siteAreaIterator = myworkspace.findByName(DocumentTypes.SiteArea,
					site);
			// get siteArea
			siteArea = siteAreaIterator.next();

			/*
			 * contentSearch(DocumentId templateId, DocumentId[] siteAreaIds,
			 * DocumentId[] categoryIds, java.lang.String[] keywords, boolean
			 * matchAllCategories, boolean includeAncestorCategories, boolean
			 * includeDescendentCategories, boolean includeAncestorSiteAreas,
			 * boolean includeDescendentSiteAreas, java.lang.String sortKey1,
			 * java.lang.String sortKey2, java.lang.String sortKey3, boolean
			 * sortAscending, int workflowStatusFilter)
			 */

			DocumentIdIterator<Content> docContent = myworkspace.contentSearch(
					authoringTmpl, new DocumentId[] { siteArea }, null, null,
					false, false, false, false, false,
					Workspace.SORT_KEY_PUBLISH_DATE, null, null, false,
					Workspace.WORKFLOWSTATUS_PUBLISHED);

			// scan document
			while (docContent.hasNext()) {
				contentid = docContent.next();
				content = myworkspace.getById(contentid);

				if (content.getName().equals(fromPage)) {

					while (docContent.hasNext() && k <= pageSize) {
						contentid = docContent.next();
						content = myworkspace.getById(contentid);
						
						String url = "";
						String label = "";
						/* khoi tao jsonobject */
						json = new JSONObject();

						label = content.getTitle();

						url = DETAIL_JSP_WCM_WPS_WCM_CONNECT + library + "/"
								+ library + "/" + site + "/"
								+ content.getName();

						json.put(LABEL_DOC, label);
						json.put(MODIFIED_DATE, DateUtil.getDate(
								content.getModifiedDate(),
								DateUtil.SHORT_DATE_PATTERN));
						json.put(HREF, url);
						// add vao mang jsonarray
						jsonArray.add(json);
						
						k++;
					}
					break;
				}
			}

			out.print(jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

}
